#!/bin/bash
#sh brances_info_get.sh -envInfoF "${Environment_FILE_PATH}" -branchInfoF "${Branch_Info_FILE_PATH}" -rebaseBranch "${REBASE_BRANCH}"
#sh brances_info_get.sh -envInfoF "../example_packing_info/app_info.json" -branchInfoF "../example_packing_info/app_branch_info.json"
:<<!
获取本分支在指定的rebase分支后所含的所有分支的详细信息:更新/添加当前包的分支信息package_merger_branchs到指定${Branch_Info_FILE_PATH}文件中
!
#responseResult=$(git log --graph --pretty=format:'%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --after "10-12-2022" --merges )
#responseResult=$(git log --pretty=format:'%s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --after "10-12-2022" --merges)

#responseResult=$(git log --pretty=format:'%C(yellow)%H' --after "10-12-2022" --no-merges )

#git log --pretty=format:'-%C(yellow)%d%Creset %s' --after "2022-10-16"



# echo "===========进入脚本$0==========="

JQ_EXEC=`which jq`

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}

# 定义颜色常量
NC='\033[0m' # No Color
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'

function logSuccessMsg() {
    printf "${GREEN}----------------  $1  ----------------${GREEN}\n${NC}"
    # echo "----------------------------.feature_brances----------------------------"
}

function debug_log() {
    if [ "${isRelease}" == true ]; then
        echo "$1"
    fi
}


# shell 参数具名化
show_usage="args: [-envInfoF, -branchInfoF, -rebaseBranch]\
                                  [--environment-info-json-file=, --branch-info-json-file=, --rebase-branch]"

while [ -n "$1" ]
do
        case "$1" in
                -envInfoF|--environment-info-json-file) Environment_FILE_PATH=$2; shift 2;;
                -branchInfoF|--branch-info-json-file) Branch_Info_FILE_PATH=$2; shift 2;;
                -rebaseBranch|--rebase-branch) REBASE_BRANCH=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

if [ ! -f "${Environment_FILE_PATH}" ];then
    echo "❌:您的Environment_FILE_PATH=${Environment_FILE_PATH}文件不存在，请检查！"
    exit_script
fi

if [ ! -f "${Branch_Info_FILE_PATH}" ];then
    echo "❌:您的Branch_Info_FILE_PATH=${Branch_Info_FILE_PATH}文件不存在，请检查！"
    exit_script
fi

# echo "rebase的分支REBASE_BRANCH=${REBASE_BRANCH}"
if [ -z "${REBASE_BRANCH}" ] || [ ${#REBASE_BRANCH} -eq 0 ];then
    echo "❌:您需要指定分支的rebase分支未填写或不存在，请检查！(附:暂时还无法直接获取到当前分支的rebase分支)"
    exit_script
fi



# 获取倒数第一个参数和倒数第二个参数，如果有的话
argCount=$#
if [ $argCount -ge 1 ]; then
    last_index=$((argCount))
    last_arg=${!last_index} # 获取倒数第一个参数
    if [ $argCount -ge 2 ]; then
        second_last_index=$((argCount - 1))
        second_last_arg=${!second_last_index} # 获取倒数第二个参数
    fi
fi
# echo "========second_last_arg=${second_last_arg}"
# echo "========       last_arg=${last_arg}"

verboseStrings=("--verbose" "-verbose") # 输入哪些字符串算是想要日志
# 判断最后一个参数是否是 verbose
if echo "${verboseStrings[@]}" | grep -wq -- "$last_arg"; then
    verbose=true
else # 最后一个元素不是 verbose
    verbose=false
fi

function log_msg() {
    if [ ${verbose} == true ]; then
        printf "$1\n"
    fi
}



log_msg "获取当前分支的所有分支信息"

JQ_EXEC=`which jq`

CurrentDirName=${PWD##*/}

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute}/..
bulidScriptCommon_dir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
# echo "bulidScriptCommon_dir_Absolute=${bulidScriptCommon_dir_Absolute}"

qscript_path_get_filepath="${bulidScriptCommon_dir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}


currentBranch=$(git branch --show-current) # 获取当前分支
currentBranchStringLength=${#currentBranch}
if [ ${currentBranchStringLength} == 0 ]; then
    echo "提示：此电脑环境未获取到本地分支(可能是jenkins问题),故将直接取直接传进来的分支"
    currentBranch=$(cat ${Environment_FILE_PATH} | ${JQ_EXEC} '.package_from_brance' | sed 's/\"//g')
fi
currentBranch=${currentBranch##*/} # 取最后的component
# echo "currentBranch=${currentBranch}"


source ${bulidScriptCommon_dir_Absolute}/a_function.sh ${bulidScriptCommon_dir_Absolute}
check_jsonFile_valid ${Environment_FILE_PATH}
check_jsonFile_valid ${Branch_Info_FILE_PATH}



# 检查是否所有的分支名都有对应的描述信息可以获取
function checkBranchNameHasDeclareInfo() {
    allContainBranchNames=($1) #记得外层加()转成数组
    BRANCH_DETAIL_INFO_FILE_PATH=$2

    featureBrancesString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.feature_brances')
    if [ "${featureBrancesString}" == null ]; then
        echo "❌Error: 您要从包含所有分支详细信息的文件中，获取所有指定分支的详细分支信息。却未获取到该文件中有任何开发分支的分支信息，所以请检查 ${BRANCH_DETAIL_INFO_FILE_PATH} 的 feature_brances 属性的值."
        return 1
    fi
    

    # echo "\n\n\n"
    # echo "$FUNCNAME 检查是否所有的分支名都有对应的描述信息可以获取"
    #featureBrancesString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.feature_brances' | sed 's/\"//g')
    #echo "featureBrancesString=\n${featureBrancesString}\n\nfeatureBrancesStringCount=${#featureBrancesString[@]}"
    #featureBranceMapArray=(${featureBrancesString}) # 注意:此处必须转为数组，才能计算出个数
    #echo "featureBranceMapArray=\n${featureBranceMapArray}\n\nfeatureBranceMapArrayCount=${#featureBranceMapArray[@]}"
    featureBranceNamesString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.feature_brances' | ${JQ_EXEC} '.[].name')
    if [ $? != 0 ]; then
        echo "❌Error: $FUNCNAME 执行出错了，您的[${BRANCH_DETAIL_INFO_FILE_PATH}]JSON文件中没有 feature_brances 属性的值，。。。请检查"
        return 1
    fi
    featureBranceNameArray=(${featureBranceNamesString}) # 字符串转数组
    featureBranceNameCount=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.feature_brances' | ${JQ_EXEC} '.' | jq 'length')
    debug_log "featureBranceNameCount=${featureBranceNameCount}, featureBranceNameArray=${featureBranceNameArray[*]}}"




    nocodeBrancesString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.nocode_brances')
    if [ "${nocodeBrancesString}" == null ]; then
        echo "${RED}❌Error: 执行 $FUNCNAME 执行出错了，您的 ${BLUE}${BRANCH_DETAIL_INFO_FILE_PATH} ${RED}这个JSON文件中没有 ${BLUE}.nocode_brances ${RED}属性的值，。。。请至少为 nocode_brances 添加包含master分支的详细信息(不写在上面 .feature_brances 属性是为了区分分支类型，只是方便查看)${NC}"
        return 1
    fi
    nocodeBranceNamesString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.nocode_brances' | ${JQ_EXEC} '.[].name')
    if [ $? != 0 ]; then
        echo "❌Error: $FUNCNAME 执行出错了，您的[${BRANCH_DETAIL_INFO_FILE_PATH}]JSON文件中没有 nocode_brances 属性的值，。。。请至少为 nocode_brances 添加包含master分支的详细信息(不写在上面 .feature_brances 属性是为了区分分支类型，只是方便查看)"
        return 1
    fi
    nocodeBranceNamesArray=(${nocodeBranceNamesString}) # 字符串转数组
    nocodeBranceNamesCount=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} '.nocode_brances' | ${JQ_EXEC} '.' | jq 'length')
    debug_log "nocodeBranceNamesCount=${nocodeBranceNamesCount}, nocodeBranceNamesArray=${nocodeBranceNamesArray[*]}"



    # echo "\n\n\n"
    debug_log "\n------判断所开发的分支，是否有未注明其需求内容的，如果有，则请先注明------"
    #[shell判断变量是否属于数组](https://blog.csdn.net/weixin_34413065/article/details/91705463?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-91705463-blog-122582351.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-91705463-blog-122582351.pc_relevant_default&utm_relevant_index=2)
    #matchMergeBranchNames=("dev_wish_3.0" "dev_will_publish" "dev_search" "dev_1.0.0_fix" "dev_hundred_wishes_new")
    #echo "matchMergeBranchNames=${matchMergeBranchNames[*]}\nmatchMergeBranchNamesCount=${#matchMergeBranchNames[@]}"
    #exit


    # 获取缺少在app_info.json中声明的所有分支名
    allContainBranchNameCount=${#allContainBranchNames[@]}
    # echo "先检查是否在${BRANCH_DETAIL_INFO_FILE_PATH}中有未声明的分支名，并用数组返回"
    for ((i=0;i<allContainBranchNameCount;i++))
    do
        devBranchName=${allContainBranchNames[i]}
        #echo "devBranchName=${devBranchName}"
        
    #    isContainInArray "${featureBranceNameArray}" "${devBranchName}"
    #    if [ $? == 0 ]; then
        if echo "${featureBranceNameArray[@]}" | grep -wq "${devBranchName}" &>/dev/null; then
            containInFeature='true'
        else
            containInFeature='false'
        fi
        
    #    isContainInArray "${nocodeBranceNamesArray}" "${devBranchName}"
    #    if [ $? == 0 ]; then
        if echo "${nocodeBranceNamesArray[@]}" | grep -wq "${devBranchName}" &>/dev/null; then
            containInNoCode='true'
        else
            containInNoCode='false'
        fi

            
        #if [[ "${featureBranceNameArray[*]}" =~ "${devBranchName}" ]]; then
        if [[ "${containInFeature}" == "true" ]]; then
            debug_log "$((i+1)) 包含${devBranchName}"
        #elif [[ "${nocodeBranceNamesArray[*]}" =~ "${devBranchName}" ]]; then
        elif [[ "${containInNoCode}" == "true" ]]; then
            debug_log "$((i+1)) 包含${devBranchName}"
        # elif [ "${devBranchName}" == "${currentBranch}" ]; then
        #     echo "$((i+1)) ${devBranchName}是当前分支，自然包含"
        else
            echo "$((i+1)) 已丢失${devBranchName},即未在${BRANCH_DETAIL_INFO_FILE_PATH}中找到${devBranchName}分支的详细信息。继续检查，找到所有，一次性输出"
            missingDeclareBranchNameArray[${#missingDeclareBranchNameArray[@]}]=${devBranchName}
        fi
    done
    # echo "missingDeclareBranchNameArray=${missingDeclareBranchNameArray[*]}"
    if [ -n "${missingDeclareBranchNameArray}" ]; then
        echo "❌ 在 $FUNCNAME 中检查发现有分支丢失填写详细信息，详情如下："
        # echo "featureBranceNameCount=${featureBranceNameCount}, featureBranceNameArray=${featureBranceNameArray[*]}"
        # echo "nocodeBranceNamesCount=${nocodeBranceNamesCount}, nocodeBranceNamesArray=${nocodeBranceNamesArray[*]}"
        PackageErrorCode=-1
        PackageErrorMessage="您所开发的有${#missingDeclareBranchNameArray[@]}个分支(详见文尾附2)，未在${BRANCH_DETAIL_INFO_FILE_PATH}文件中标明功能(标明方法见文尾,👉🏻提示:如有添加请检查①是不是本地没拉到代码,②是不是name写错了)。从而会导致自动化打包时候无法获取，从而提供所打包的所含功能说明。故请前往补充后再执行打包。\n附1：标明方法①(推荐)前往项目的 featureBrances ，在该目录下添加一个描述该分支的json文件信息；标明方法②(不推荐)直接在${BRANCH_DETAIL_INFO_FILE_PATH}文件中的 featureBrances 属性里添加。\n附2：缺少标注功能的分支分别为${missingDeclareBranchNameArray[*]}分支。"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        return 1
    fi
    
    debug_log "恭喜💐:您新提交的那些分支都能够从json文件中获取到分支信息"
    return 0
}



function getAndAddTestState() {
    while [ -n "$1" ]
    do
            case "$1" in
                    -branchMap|--branchMap) branchMap=$2; shift 2;;
                    --) break ;;
                    *) echo $1,$2; break ;;
            esac
    done
    
    echo "branchMap000=${branchMap}"
    #提交测试的时间
    branchSubmitTestTime=$(echo ${branchMap} | ${JQ_EXEC} -r ".submit_test_time") # -r 去除字符串引号
    if [ "${branchSubmitTestTime}" != "null" ] && [ -n "${branchSubmitTestTime}" ]; then
        testState='test_submit'
    fi
    
    #通过测试的时间
    branchPassTestTime=$(echo ${branchMap} | ${JQ_EXEC} -r ".pass_test_time") # -r 去除字符串引号
    if [ "${branchPassTestTime}" != "null" ] && [ -n "${branchPassTestTime}" ]; then
        testState='test_pass'
    fi
    
    #合入预生产的时间
    branchMergerPreproductTime=$(echo ${branchMap} | ${JQ_EXEC} -r ".merger_pre_time") # -r 去除字符串引号
    if [ "${branchMergerPreproductTime}" != "null" ] && [ -n "${branchMergerPreproductTime}" ]; then
        testState='test_prefect'
    fi
    
#    branchMap=(${branchMap})
    branchMap["testState"]=${testState}
    echo "TODO:此方法尚未调试通过branchMap111=${branchMap}"
    
    branchMapResult=${branchMap}
    echo "branchMapResult=${branchMap},testState=${testState}"
}

# 从包含所有分支详细信息的文件中，获取单个指定分支的详细分支信息
function getSingleBranchDetailInfoFromFile() {
    BRANCH_DETAIL_INFO_FILE_PATH=$1
    devBranchName=$2

    branceDetailMapResult="null"
    # if [[ "${featureBranceNameArray[*]}" =~ "${devBranchName}" ]]; then
    if echo "${featureBranceNameArray[@]}" | grep -wq "${devBranchName}" &>/dev/null; then # 包含
        #devBranceMapString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r '.feature_brances[] | select(.name=="dev_will_publish")')
        devBranceMapString=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r ".feature_brances[] | select(.name==\"${devBranchName}\")")
        #echo "devBranceMapString=${devBranceMapString}"

        #echo -n '{"beans":[{"name":"ac"},{"name":"bc", "age":23}] }' | jq -r '.beans[]|select(.name=="bc")|.age'
        #devBranceName=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r ".feature_brances[] | select(.name==\"${devBranchName}\") |.name")
        devBranceMapArray=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r ".feature_brances[] | select(.name==\"${devBranchName}\")")
        if [ -z "${devBranceMapArray}" ]; then
            echo "❌Error:获取分支的详细信息失败：《 cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r \".feature_brances[] | select(.name==\"${devBranchName}\")\"》，请检查补充"
            return 1
        fi
        #echo "devBranceMapArray=${devBranceMapArray}"
    # elif [[ "${nocodeBranceNamesArray[*]}" =~ "${devBranchName}" ]]; then
    elif echo "${nocodeBranceNamesArray[@]}" | grep -wq "${devBranchName}" &>/dev/null; then # 包含
        devBranceMapArray=$(cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r ".nocode_brances[] | select(.name==\"${devBranchName}\")")
        #echo "devBranceMapArray=${devBranceMapArray}"
        if [ -z "${devBranceMapArray}" ]; then
            echo "❌Error:获取分支的详细信息失败：《 cat ${BRANCH_DETAIL_INFO_FILE_PATH} | ${JQ_EXEC} -r \".nocode_brances[] | select(.name==\"${devBranchName}\")\"》，请检查补充"
            return 1
        fi
        
    # elif [ "${devBranchName}" == "${currentBranch}" ]; then
    #     echo "${devBranchName}是当前分支"
    #     continue
    else
        echo "获取分支的详细信息失败：在${BRANCH_DETAIL_INFO_FILE_PATH}中的 .feature_brances[] 和 .nocode_brances[] 都未找到本分支${devBranchName}的功能信息，请检查补充"
        return 1
    fi

    devBranceNamesString=$(echo ${devBranceMapArray} | ${JQ_EXEC} -r '.name') # -r 去除字符串引号
    devBranceNameArray=(`echo ${devBranceNamesString}`)
    devBranceMapCount=${#devBranceNameArray[@]}
    #echo "devBranceMapCount=${devBranceMapCount}"
    if [ ${devBranceMapCount} -gt 1 ]; then
        PackageErrorCode=-1
        PackageErrorMessage="检查到 ${BRANCH_DETAIL_INFO_FILE_PATH} 中有${devBranceMapCount}个${devBranchName}分支，请方法①合并为一个或方法②将之前已上线的需求功能移入history_versions对应的版本里，使其只剩一个"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        return 1
    fi

    branceDetailMapResult=${devBranceMapArray}
}


# 从包含所有分支详细信息的文件中，获取所有指定分支的详细分支信息
function getAllBranchDetailInfoFromFile() {
    BRANCH_DETAIL_INFO_FILE_PATH=$1
    allContainBranchNames=$2
    # echo "$FUNCNAME ✅allContainBranchNames=${allContainBranchNames[*]}"
    
    debug_log "正在执行命令(检查以下所有的分支名在文件中是否都有对应的描述信息可以获取):《 checkBranchNameHasDeclareInfo \"${allContainBranchNames[*]}\" \"${BRANCH_DETAIL_INFO_FILE_PATH}\" 》"
    checkBranchNameHasDeclareInfo "${allContainBranchNames[*]}" "${BRANCH_DETAIL_INFO_FILE_PATH}"
    if [ $? != 0 ]; then
        return 1
    fi
    
    echo "\n"
    debug_log "所有分支描述已填写验证通过，现在正式从 ${BRANCH_DETAIL_INFO_FILE_PATH} 中获取所有指定分支 [${allContainBranchNames[*]}] 的详细分支信息."
    allContainBranchNameCount=${#allContainBranchNames[@]}
    for ((i=0;i<allContainBranchNameCount;i++))
    do
        devBranchName=${allContainBranchNames[i]}
        
        getSingleBranchDetailInfoFromFile "${BRANCH_DETAIL_INFO_FILE_PATH}" "${devBranchName}"
        if [ $? != 0 ]; then
            return 1
        fi
        devBranceMap=${branceDetailMapResult}
    #    getAndAddTestState -branchMap "${devBranceMap}"
    #    devBranceMap=${branchMapResult}
        
        debug_log "执行《 getSingleBranchDetailInfoFromFile \"${BRANCH_DETAIL_INFO_FILE_PATH}\" \"${devBranchName}\" 》 得到 devBranceMap=${devBranceMap[*]}"
        packageBranchMapArray[${#packageBranchMapArray[@]}]=${devBranceMap}
    done
    debug_log "您包含的 ${BLUE}${#packageBranchMapArray[@]} ${GREEN}个分支 ${BLUE}${allContainBranchNames[*]} ${GREEN}的所有分支信息(未排序)如下:\n${BLUE}${packageBranchMapArray[*]}"


    packageBranchNameCount=${#packageBranchMapArray[@]}
    if [ ${packageBranchNameCount} == 0 ]; then
        echo "❌Error:分支个数为空，会导致下面取packageBranchMapString时候错误"
        return 1
    fi


    if [ ${packageBranchNameCount} == 1 ]; then
        devBranceMap=${packageBranchMapArray[0]}
        debug_log "------------------0:${devBranceMap}"
        packageBranchMapString="["
        packageBranchMapString+=${devBranceMap}
        packageBranchMapString+="]"
    else
        for ((i=0;i<${packageBranchNameCount};i++))
        do
            devBranceMap=${packageBranchMapArray[i]}
            debug_log "------------------${i}:${devBranceMap}"
            if [ -z "${devBranceMap}" ]; then
                echo "❌Error:从${packageBranchMapArray[*]}中获取索引$((i))的分支的内容为空，请检查"
                return 1
            fi
            if [ ${i} == 0 ]; then
                packageBranchMapString="["
            fi
            
            packageBranchMapString+=${devBranceMap}
            
            if [ ${i} -lt $((packageBranchNameCount-1)) ]; then
                packageBranchMapString+=","
            fi
            
            if [ ${i} == $((packageBranchNameCount-1)) ]; then
                packageBranchMapString+="]"
            fi
        done
    fi
    # TODO:
    #getJsonStringFromArray ${packageBranchMapArray[*]}
    #resultCode=$?
    #if [ ${resultCode} != 0 ]; then
    #    echo "$0 数组转json字符串失败"
    #    exit_script
    #fi
    #packageBranchMapString=${arrayJsonResultString}


    packageBranchMapString_withjq_origin=$(echo "${packageBranchMapString}" | ${JQ_EXEC} -r ".")
    #echo "==========要添加的json文件排序前:packageBranchMapString_withjq_origin=${packageBranchMapString_withjq_origin}"
    source ${CurrentDIR_Script_Absolute}/brances_sort_function.sh ${bulidScriptCommon_dir_Absolute}
    sortBranchArrayByType -branchMaps "${packageBranchMapString_withjq_origin}"
    packageBranchMapString_withjq_sort=${branchMapArray_all_new_withjq_result}
    #echo "==========要添加的json文件排序后:packageBranchMapString_withjq_sort=${packageBranchMapString_withjq_sort}"
    debug_log "恭喜:您包含的 ${BLUE}${#packageBranchMapArray[@]} ${GREEN}个分支 ${BLUE}${allContainBranchNames[*]} ${GREEN}的所有分支信息(排序后)如下:${BLUE}packageBranchMapString_withjq_sort=${packageBranchMapString_withjq_sort}"

    
}



log_msg "${YELLOW}正在执行命令(获取当前分支的rebase日期和该日期后的所有合入分支名)：《 ${BLUE}sh ${bulidScriptCommon_dir_Absolute}/all_merger_branch_name/get_all_merger_branch_names.sh -currentBranch \"${currentBranch}\" -rebaseFrom \"${REBASE_BRANCH}\" ${YELLOW}》${NC}"
allContainBranchNamesJsonResult=$(sh ${bulidScriptCommon_dir_Absolute}/all_merger_branch_name/get_all_merger_branch_names.sh -currentBranch "${currentBranch}" -rebaseFrom "${REBASE_BRANCH}")
if [ $? != 0 ]; then
    echo "${allContainBranchNamesJsonResult}" # 此时值为出错原因
    exit_script
fi
allContainBranchNamesResult=$(echo "$allContainBranchNamesJsonResult" | jq -r '.data')
allContainBranchNames=(${allContainBranchNamesResult[*]}) #记得外层加()转成数组

echo "\n"
log_msg "正在执行命令(从包含所有分支详细信息的文件中，获取所有指定分支的详细分支信息):《 getAllBranchDetailInfoFromFile \"${Branch_Info_FILE_PATH}\" \"${allContainBranchNames}\" 》"
getAllBranchDetailInfoFromFile "${Branch_Info_FILE_PATH}" "${allContainBranchNames}"
if [ $? != 0 ]; then
    exit_script
fi

sh ${bulidScriptCommon_dir_Absolute}/update_json_file.sh -f "${Branch_Info_FILE_PATH}" -k "package_merger_branchs" -v "${packageBranchMapString_withjq_sort}"
if [ $? != 0 ]; then
    PackageErrorCode=-1
    PackageErrorMessage="❌更新/添加当前包的分支信息package_merger_branchs到指定${Branch_Info_FILE_PATH}文件中失败啦(本脚本文件是$0)"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi


# logSuccessMsg "恭喜:最后的 .package_merger_branchs 值如下"
logSuccessMsg "恭喜:您包含的 ${BLUE}${#packageBranchMapArray[@]} ${GREEN}个分支 ${BLUE}${allContainBranchNames[*]} ${GREEN}的所有分支信息(排序后)，已存入 ${BLUE}${Branch_Info_FILE_PATH} ${GREEN}的 ${BLUE}.package_merger_branchs ${GREEN}字段中，其值如下:"
cat ${Branch_Info_FILE_PATH} | jq ".package_merger_branchs" | jq '.'
